var app = getApp();
Page({
  data: {
    img_height: 1,
    productTypeId:0,
    type:0,
    img:"../../img/group.png",
    imgtype:0,
  },
  onLoad: function (options) {
    let that = this
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight,
      parentTypeId: options.typeId,
      text: options.text
    })
    console.info(that.data.text);
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          img_height: res.windowHeight - app.globalData.navHeight,
        })
      }
    })
    wx.request({
      url: app.globalData.url + "searchListByES",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        searchCondition: that.data.text,
        type: 0,
        parentTypeId: 0,
        userId: wx.getStorageSync("userId")
      },
      method: "POST",
      success: function (res) {
        var data = res.data;
        if (data.result === 1) {
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].promotionMsg != undefined) {
              var arr = res.data.data[i].promotionMsg.split(",");
              res.data.data[i].arr = arr;
            } else {
              res.data.data[i].arr = [];
            }
            if (res.data.data[i].promotionType != undefined) {
              //先按照逗号分割字符串
              let arrycode = res.data.data[i].promotionType.split(",");
              //去重
              arrycode = Array.from(new Set(arrycode));
              //拼接正确的nextOperatorCode
              res.data.data[i].promotionType = arrycode = arrycode.toLocaleString();
              // var promotionType = res.data.data[i].promotionType.split(",");
              // res.data.data[i].promotionType = promotionType;
            } else {
              res.data.data[i].promotionType = [];
            }
          }
          console.info(res.data.data);
          that.setData({
            productRecommend: data.data,
          })
        }
      }
    });
  },
  find: function (e) {
    var that = this;
    console.log(e.detail.value)
    that.setData({
      text: e.detail.value,
    })
    if (e.detail.value != ""){
      wx.request({
        url: app.globalData.url + "searchListByES",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          searchCondition: e.detail.value,
          type: 0,
          parentTypeId: 0,
          userId: wx.getStorageSync("userId")
        },
        method: "POST",
        success: function (res) {
          var data = res.data;
          if (data.result === 1) {
            for (var i = 0; i < res.data.data.length; i++) {
              var arr = [];
              if (res.data.data[i].promotionMsg != undefined && res.data.data[i].promotionMsg != 'undefined') {
                arr = res.data.data[i].promotionMsg.split(",");
              }
              res.data.data[i].arr = arr;
              if (res.data.data[i].promotionType != undefined) {
                //先按照逗号分割字符串
                let arrycode = res.data.data[i].promotionType.split(",");
                //去重
                arrycode = Array.from(new Set(arrycode));
                //拼接正确的nextOperatorCode
                res.data.data[i].promotionType = arrycode = arrycode.toLocaleString();
                // var promotionType = res.data.data[i].promotionType.split(",");
                // res.data.data[i].promotionType = promotionType;
              } else {
                res.data.data[i].promotionType = [];
              }
            }
            that.setData({
              productRecommend: data.data,
            })
          }
        }
      });
    }else{
      that.productF(that.data.productTypeId);
    }
}, 
  navBack: function () {
    wx.navigateBack({

    })
  },
  choose: function (e) {
    var that = this;
    if (e.currentTarget.dataset.type == 2){
      if(this.data.imgtype==2){
        console.log(this.data.imgtype)
        this.setData({
          type: e.currentTarget.dataset.type,
          img:"../../img/group1.png",
          imgtype:1
      })
        wx.request({
          url: app.globalData.url + "searchListByES",
          header: {
            "content-type": "application/x-www-form-urlencoded"
          },
          data: {
            searchCondition: that.data.text,
            type: 2,
            parentTypeId: 0,
            userId: wx.getStorageSync("userId")
          },
          method: "POST",
          success: function (res) {
            var data = res.data;
            if (data.result === 1) {
              for (var i = 0; i < res.data.data.length; i++) {
                var arr = [];
                if (res.data.data[i].promotionMsg != undefined && res.data.data[i].promotionMsg != 'undefined') {
                  arr = res.data.data[i].promotionMsg.split(",");
                }
                res.data.data[i].arr = arr;
                if (res.data.data[i].promotionType != undefined) {
                  //先按照逗号分割字符串
                  let arrycode = res.data.data[i].promotionType.split(",");
                  //去重
                  arrycode = Array.from(new Set(arrycode));
                  //拼接正确的nextOperatorCode
                  res.data.data[i].promotionType = arrycode = arrycode.toLocaleString();
                  // var promotionType = res.data.data[i].promotionType.split(",");
                  // res.data.data[i].promotionType = promotionType;
                } else {
                  res.data.data[i].promotionType = [];
                }
              }
              console.info(res.data.data);
              that.setData({
                productRecommend: data.data,
              })
            }
          }
        });
      } else{
        this.setData({
          type: e.currentTarget.dataset.type,
          img: "../../img/group2.png",
          imgtype: 2
        })
        wx.request({
          url: app.globalData.url + "searchListByES",
          header: {
            "content-type": "application/x-www-form-urlencoded"
          },
          data: {
            searchCondition: that.data.text,
            type: 3,
            parentTypeId: 0,
            userId: wx.getStorageSync("userId")
          },
          method: "POST",
          success: function (res) {
            var data = res.data;
            if (data.result === 1) {
              for (var i = 0; i < res.data.data.length; i++) {
                var arr = [];
                if (res.data.data[i].promotionMsg != undefined && res.data.data[i].promotionMsg != 'undefined') {
                  arr = res.data.data[i].promotionMsg.split(",");
                }
                res.data.data[i].arr = arr;
                if (res.data.data[i].promotionType != undefined) {
                  //先按照逗号分割字符串
                  let arrycode = res.data.data[i].promotionType.split(",");
                  //去重
                  arrycode = Array.from(new Set(arrycode));
                  //拼接正确的nextOperatorCode
                  res.data.data[i].promotionType = arrycode = arrycode.toLocaleString();
                  // var promotionType = res.data.data[i].promotionType.split(",");
                  // res.data.data[i].promotionType = promotionType;
                } else {
                  res.data.data[i].promotionType = [];
                }
              }
              console.info(res.data.data);
              that.setData({
                productRecommend: data.data,
              })
            }
          }
        });
      }
    }else{
      this.setData({
        type: e.currentTarget.dataset.type,
        img: "../../img/group.png",
        imgtype: 0
      })
      wx.request({
        url: app.globalData.url + "searchListByES",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          searchCondition: that.data.text,
          type: that.data.type,
          parentTypeId: 0,
          userId: wx.getStorageSync("userId")
        },
        method: "POST",
        success: function (res) {
          var data = res.data;
          if (data.result === 1) {
            for (var i = 0; i < res.data.data.length; i++) {
              var arr = [];
              if (res.data.data[i].promotionMsg != undefined&&res.data.data[i].promotionMsg!='undefined'){
                 arr = res.data.data[i].promotionMsg.split(",");
              }
              res.data.data[i].arr = arr;
              if (res.data.data[i].promotionType != undefined) {
                //先按照逗号分割字符串
                let arrycode = res.data.data[i].promotionType.split(",");
                //去重
                arrycode = Array.from(new Set(arrycode));
                //拼接正确的nextOperatorCode
                res.data.data[i].promotionType = arrycode = arrycode.toLocaleString();
                // var promotionType = res.data.data[i].promotionType.split(",");
                // res.data.data[i].promotionType = promotionType;
              } else {
                res.data.data[i].promotionType = [];
              }
            }
            console.info(data.data);
            that.setData({
              productRecommend: data.data,
            })
          }
        }
      });
    }
    
  },
  productF: function (productTypeId) {
    var that = this;
    wx.request({
      url: app.globalData.url + "classificationGoods",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        productTypeId: productTypeId,
        userId: wx.getStorageSync("userId")
      },
      method: "POST",
      success: function (res) {
        var data = res.data;
        if (data.result === 1) {
          that.setData({
            product: data.data,
          })
        }
      }
    });
  },
  receive: function (e) {
    console.info(JSON.stringify(this.data.product[e.target.dataset.index]));
    wx.navigateTo({
      url: '/pages/productDetail/productDetail?productid=' + e.currentTarget.dataset.productid + "&product=" + JSON.stringify(this.data.product[e.target.dataset.index]),
    })
  },
  /**
 * 跳转到产品详情
 */
  details: function (res) {
    var productid = res.currentTarget.dataset.productid;
    wx.navigateTo({
      url: '/pages/productDetail/productDetail?productid=' + productid+'&type=1',
    })

  },
  deltext:function(){
    this.setData({
      text:""
    })
  },
  // 立即购买
  immeBuy(e) {
    var that = this;
    wx.request({
      url: app.globalData.url + "modifyShoppingCart",//给函数传递服务器地址参数 
      data: {
        productId: e.currentTarget.dataset.productid,
        userId: wx.getStorageSync("userId"),
        num: 1,
      },//给服务器传递数据，本次请求不需要数据，可以不填
      header: {
        'content-type': 'application/json' // 默认值，返回的数据设置为json数组格式
      },
      success: function () {//打印出返回的数据
        //that.toCar();
        // that.toCar();
        wx.showToast({
          title: '加入购物车成功',
          icon: 'success',
          duration: 2000
        });
        /**
         * 购物车数量
         */
        wx.request({
          url: app.globalData.url + "countShoppingCartNum",
          method: "GET",
          data: {
            userId: wx.getStorageSync("userId"),
          },
          success: function (res) {
            var data = res.data.data;
            that.setData({
              cartCount: data
            })
          }
        });
      }
    })
  },
})
